<template>
  <div class="order">
    <!-- 自定义导航栏 -->
    <gh-navbar id="navbar" :showback="true" title="咨询订单" url="/pages/index/index" />
    <div class="bar"></div>
    <div class="serve box">
      <div class="title">{{categroy}}服务</div>
      <div class="serve-content">
        <div class="left">
          <div class="price">￥{{money}}</div>
          <div class="time label">{{time}}</div>
        </div>
        <div class="right">
          <navigator class="state" url="/pageIndex/menu/explain">说明</navigator>
        </div>
      </div>
    </div>
    <div class="box">
      <div class="item">
        <div class="time label">{{categroy}}</div>
        <div class="price">￥{{money}}元</div>
      </div>
    </div>
    <div class="box">
      <div class="item">
        <div class="label">
          <text>佣金抵扣</text>
          <text class="commission">{{real_commission}}元</text>
        </div>
        <checkbox-group @change="handleCheck">
          <checkbox class="check" color="#ABB2CE" value="true" />
        </checkbox-group>
      </div>
    </div>
    <div class="box">
      <div class="item">
        <div class="time label">共需支付</div>
        <div class="price">￥{{totalMoney}}元</div>
      </div>
    </div>
    <button class="submit" @click="handlePay">确认支付</button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      categroy: "在线咨询",
      money: 20,
      onlineMoney: 20,
      commission: "18.00",
      check: false,
      time: "",
      id: 0, // 订单id
      type: 0, // 订单类型  在线。。。
      serve: '', // 等于2时，是一对一下单
    };
  },
  computed: {
    real_commission() {
      const dikou = this.commission > this.money ? this.money : this.commission;
      return dikou;
    },
    totalMoney() {
      let temp = this.check ? this.money - this.real_commission : this.money;
      return temp;
    }
  },
  async onLoad(e) {
    this.id = e.id;
    this.serve = e.serve
    const res = await this.$gh.request("member.order", {
      data: {
        op: "getPreOrder",
        consultation_type: e.type
      }
    });
    const {
      charge,
      duration,
      time_unit,
      member,
      consultation_type
    } = res.data.list;
    this.money = parseFloat(charge);
    const unit = time_unit == "minute" ? "分钟" : "天";
    this.time = duration + unit;
    this.commission = parseFloat(member.credit1);
    this.type = parseInt(consultation_type);
    switch (parseInt(consultation_type)) {
      case 1:
        this.categroy = "在线咨询";
        break;
      case 2:
        this.categroy = "电话咨询";
        break;
      case 3:
        this.categroy = "合同起草";
        break;
      case 4:
        this.categroy = "律师函";
        break;
      case 5:
        this.categroy = "私人顾问";
        break;

      default:
        break;
    }
  },
  methods: {
    async handlePay() {
      let deduction = this.check ? 1 : 0;
      if(this.type == 1 && this.serve != 2){
        await this.$store.dispatch("modal", "是否确认支付");
          // 公开咨询转付费咨询

          const res = await this.$gh
            .request("member.order", {
              data: {
                op: "editOrderPayment",
                orderid: this.id,
                deduction
              }
            })
            .catch(err => {
              uni.showToast({
                mask: true,
                icon: "none",
                title: err.errmsg,
                duration: 2000
              });
            });
          if (res.code == 200 && !res.data.list.payment_launch) {  // 佣金足够扣，不需要支付
            uni.showToast({
              mask: true,
              icon: "success",
              title: "支付成功",
              duration: 2000
            });
            uni.navigateTo({
              url: "/pageMy/menu/paidConsulting"
            });
          }else if(res.code == 200 && res.data.list.payment_launch){
            console.log(this.id);
            // 调起微信支付
            this.$gh.pay(this.id).then(() => {
              // 跳转
              uni.reLaunch({
                 url: '/pageMy/menu/paidConsulting'
              });
            }).catch(err => {
              uni.showToast({
                icon: 'none',
                title: '支付失败',
                duration: 2000
              });
            });
          }
      }else{
        // 除了公开在线咨询，其他都是请求下单确认接口
          await this.$store.dispatch("modal", "是否确认支付");
          const resDefault = await this.$gh
            .request("member.order", {
              data: {
                op: "addOrderPay",
                orderid: this.id,
                deduction
              }
            })
            .catch(err => {
              uni.showToast({
                mask: true,
                icon: "none",
                title: err.errmsg,
                duration: 2000
              });
            });
          if (resDefault.code == 200 && !resDefault.data.list.payment_launch) {  // 佣金足够扣，不需要支付
            uni.showToast({
              mask: true,
              icon: "success",
              title: "支付成功",
              duration: 2000
            });
            let url = this.serve == 2 ?  "/pageMy/menu/oneByOne" : "/pageMy/menu/paidConsulting"
            uni.navigateTo({
              url
            });
          }else if(resDefault.code == 200 && resDefault.data.list.payment_launch){
            let url = this.serve == 2 ?  "/pageMy/menu/oneByOne" : "/pageMy/menu/paidConsulting"
            // 调起微信支付
            this.$gh.pay(this.id).then(() => {
              // 跳转
              uni.reLaunch({
                 url
              });
            }).catch(err => {
              uni.showToast({
                icon: 'none',
                title: '支付失败',
                duration: 2000
              });
            });
          }
      }
    },
    handleCheck(e) {
      e.detail.value.length == 1 ? (this.check = true) : (this.check = false);
    }
  }
};
</script>
<style lang='less' scoped>
@import url("../../common/util.less");
.order {
  width: 100vw;
  height: 100vh;
  background-color: #fff;
  .bar {
    height: 10px;
    background-color: #f9faff;
  }
  .box {
    padding: 18rpx 27rpx;
    border-bottom: 2rpx solid #eee;
    .label {
      font-size: 26rpx;
      color: @font-color;
    }
    .price {
      font-size: 24rpx;
      color: red;
    }
  }
  .serve {
    .title {
      font-size: 30rpx;
      color: @deep-color;
      margin-bottom: 10rpx;
    }
    .serve-content {
      .gh-between();
      .left {
        .flex();
        .time {
          margin-left: 20rpx;
        }
      }
      .state {
        font-size: 24rpx;
        color: #fea388;
      }
    }
  }
  .item {
    .gh-between();
    .commission {
      margin-left: 20rpx;
    }
    .check {
      transform: scale(0.7);
    }
  }
  .submit {
    .gh-center();
    width: 620rpx;
    height: 80rpx;
    margin: 65rpx auto;
    background-color: #849aff;
    border-radius: 40rpx;
    color: #fff;
    font-size: 36rpx;
    border: none;
  }
}
</style>